package com.global.college.ui.fragment.order;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.global.college.R;
import com.global.college.UserData;
import com.global.college.base.baselist.BaseViewHolder;
import com.global.college.base.baselist.CommListFragment;
import com.global.college.base.baselist.CommRecyclerAdapter;
import com.global.college.constants.Constant;
import com.global.college.ui.activity.OrderDetailActivity;
import com.global.college.ui.bean.MyOrderDetail;
import com.global.college.ui.bean.MyOrderListModel;
import com.global.college.ui.bean.OrderCreateModel;
import com.global.college.ui.interfaces.IOrderView;
import com.global.college.ui.presenter.OrderPresenter;
import com.global.college.utils.StringUtils;
import com.global.college.views.CommDialog;
import com.global.college.views.PayOrderDialog;
import com.global.college.widget.FrescoImageView;
import com.scwang.smartrefresh.layout.api.RefreshLayout;

import java.util.ArrayList;
import java.util.List;

/**
 * 订单中心
 */
public class OrderCommonFragment extends CommListFragment implements IOrderView {

    private int page = Constant.page;
    private int pageSize = Constant.pageSize;
    private List<MyOrderListModel.UserOrderVoListBean> mData = new ArrayList<>();
    private static final String ARG_PARAM1 = "type";
    //type  订单状态 默认  0 未支付，1 支付完成，2 取消的，不传默认查询当前用户所有订单
    private String type;

    private OrderPresenter orderPresenter;
    private PayOrderDialog mPayOrderDialog;
    public OrderCommonFragment() {
    }

    public static OrderCommonFragment newInstance(String type) {
        OrderCommonFragment fragment = new OrderCommonFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            type = getArguments().getString(ARG_PARAM1);
        }
        orderPresenter = new OrderPresenter(mActivity);
        orderPresenter.setOrderView(this);

    }

    @Override
    public void fetchData() {
    }

    @Override
    protected void getData() {

    }

    @Override
    public void onResume() {
        super.onResume();
        //type  订单状态 默认  0 未支付，1 支付完成，2 取消的，不传默认查询当前用户所有订单
        page = 1;
        getData(type);
    }

    @Override
    protected CommRecyclerAdapter setAdapter() {
        return new CommRecyclerAdapter() {
            @Override
            protected void onBindView(BaseViewHolder holder, final int position) {
                LinearLayout llItem = holder.getView(R.id.ll_item);
                TextView tvOrderNum = holder.getView(R.id.tv_order_num);
                TextView tvOrderStatus = holder.getView(R.id.tv_order_status);
                FrescoImageView fivCover = holder.getView(R.id.fiv_cover);
//                TextView tvType = holder.getView(R.id.tv_type);
                TextView tvDiscountPrice = holder.getView(R.id.tv_discount_price);
                TextView tvPayPrice = holder.getView(R.id.tv_pay_price);
                TextView tvDetail = holder.getView(R.id.tv_detail);
                TextView tvCancle = holder.getView(R.id.tv_cancle);
                TextView tvContinuePay = holder.getView(R.id.tv_continue_pay);
                fivCover.setImageURI(mData.get(position).getGoodCover());
                tvOrderNum.setText("订单编号："+mData.get(position).getOrderId());
                tvDiscountPrice.setText(StringUtils.getPrice2(String.valueOf(mData.get(position).getGoodPreferentialPrice())));
                tvPayPrice.setText("¥"+StringUtils.getPrice2(String.valueOf(mData.get(position).getOrderPrice())));
                //订单状态默认0未支付，1支付完成，2取消的
                switch (mData.get(position).getOrderState()) {
                    case 0:
                        tvCancle.setVisibility(View.VISIBLE);
                        tvContinuePay.setVisibility(View.VISIBLE);
                        tvDetail.setVisibility(View.GONE);
                        tvOrderStatus.setText("未支付");
                        tvOrderStatus.setTextColor(mActivity.getResources().getColor(R.color.color_red));
                        break;
                    case 1:
                        tvCancle.setVisibility(View.GONE);
                        tvContinuePay.setVisibility(View.GONE);
                        tvDetail.setVisibility(View.VISIBLE);
                        tvOrderStatus.setText("已完成");
                        tvOrderStatus.setTextColor(mActivity.getResources().getColor(R.color.green_active));
                        break;
                    case 2:
                        tvCancle.setVisibility(View.GONE);
                        tvContinuePay.setVisibility(View.GONE);
                        tvDetail.setVisibility(View.VISIBLE);
                        tvOrderStatus.setText("已取消");
                        tvOrderStatus.setTextColor(mActivity.getResources().getColor(R.color.text_grey));
                        break;
                }
//                //商品类型，1课程2训练营
//                switch (mData.get(position).getGoodType()) {
//                    case 1:
//                        tvType.setText("类型：课程");
//                        break;
//                    case 2:
//                        tvType.setText("类型：训练营");
//                        break;
//                }

                llItem.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        //mActivity.startActivity(new Intent(mActivity, OrderDetailActivity.class).putExtra(Constant.Key.NAME, String.valueOf(mData.get(position).getOrderId())));
                    }
                });
                tvDetail.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        mActivity.startActivity(new Intent(mActivity, OrderDetailActivity.class).putExtra(Constant.Key.NAME, String.valueOf(mData.get(position).getOrderId())));
                    }
                });
                tvCancle.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        CommDialog.newInstance(mActivity).setClickListen(new CommDialog.TwoSelDialog() {
                            @Override
                            public void leftClick() {
                            }
                            @Override
                            public void rightClick() {
                                orderPresenter.cancleOrder(mData.get(position).getOrderId());
                            }
                        }).setTitle(mActivity.getString(R.string.str_cancle_order_tip_1)).setContent( mActivity.getString(R.string.str_cancle_order_tip_2)).show();

                    }
                });
                tvContinuePay.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        OrderCreateModel orderCreateModel = new OrderCreateModel();
                        orderCreateModel.setUserId(mData.get(position).getUserId());
                        orderCreateModel.setCancelTime(mData.get(position).getCancelTime());
                        orderCreateModel.setCreateTime(mData.get(position).getCreateTime());
                        orderCreateModel.setGoodCover(mData.get(position).getGoodCover());
                        orderCreateModel.setGoodId(mData.get(position).getGoodId());
                        orderCreateModel.setGoodName(mData.get(position).getGoodName());
                        orderCreateModel.setGoodPreferentialPrice(mData.get(position).getGoodPreferentialPrice());
                        orderCreateModel.setGoodPrice(mData.get(position).getGoodPrice());
                        orderCreateModel.setGoodType(mData.get(position).getGoodType()+"");
                        orderCreateModel.setId(mData.get(position).getId());
                        orderCreateModel.setOrderId(mData.get(position).getOrderId());
                        orderCreateModel.setOrderPrice(mData.get(position).getOrderPrice());
                        orderCreateModel.setOrderState(mData.get(position).getOrderState());
                        orderCreateModel.setPayCompleteTime(mData.get(position).getPayCompleteTime());
                        orderCreateModel.setPayTime(mData.get(position).getPayTime());
                        orderCreateModel.setSource(mData.get(position).getSource());
                        //mActivity.startActivity(new Intent(mActivity, OrderDetailActivity.class).putExtra(Constant.Key.NAME, String.valueOf(mData.get(position).getOrderId())));
                        mPayOrderDialog = PayOrderDialog.newInstance("",orderCreateModel, new PayOrderDialog.OnCallBack() {
                            @Override
                            public void callBack(String param) {
                                getData(type);
                            }
                        });
                        mPayOrderDialog.show(mActivity.getSupportFragmentManager(), "help_buy_vip");
                    }
                });
            }

            @Override
            protected int getLayoutID(int position) {
                return R.layout.item_common_order;
            }

            @Override
            public int getItemCount() {
                return mData.size();
            }
        };
    }


    @Override
    public void onLoadmore(RefreshLayout refreshlayout) {
        page++;
        getData(type);
    }

    @Override
    public void onRefresh(RefreshLayout refreshlayout) {
        page = 1;
        getData(type);
    }

    private void getData(String type) {
        if (StringUtils.equals("0", type)) {
            orderPresenter.getOrderList(UserData.getInstance().getUserId(), "", page, pageSize);
        } else {
            int mType = Integer.valueOf(type) - 1;
            orderPresenter.getOrderList(UserData.getInstance().getUserId(), String.valueOf(mType), page, pageSize);
        }

    }

    @Override
    public void onGetOrderListSuccess(MyOrderListModel model) {
        if (null != model) {
            if (page == 1) {
                refreshFinish();
                mData.clear();
                if (null != model.getUserOrderVoList() && model.getUserOrderVoList().size() > 0) {
                    mData.addAll(model.getUserOrderVoList());
                } else {
                    setEmpty(getString(R.string.cube_ptr_not_order_data), R.drawable.icon_empty);
                }
            } else {
                finishLoadmore();
                if (null != model.getUserOrderVoList() && model.getUserOrderVoList().size() > 0)
                    mData.addAll(model.getUserOrderVoList());
            }

            if (null != model.getUserOrderVoList() && model.getUserOrderVoList().size() < pageSize) {
                finishLoadmoreWithNoMoreData();
            }

            if (mAdapter != null) {
                mAdapter.notifyDataSetChanged();
            }

        } else {
            setEmpty(getString(R.string.cube_ptr_not_order_data), R.drawable.icon_empty);
        }
    }

    @Override
    public void onGetOrderListFail(String error) {

    }

    @Override
    public void onGetOrderDetailSuccess(MyOrderDetail smsModel) {

    }

    @Override
    public void onGetOrderDetailFail(String error) {

    }

    @Override
    public void onCreateOrderSuccess(Object smsModel) {

    }

    @Override
    public void onCreateOrderFail(String error) {

    }

    @Override
    public void onCancleOrderSuccess(Object smsModel) {
        page = 1;
        getData(type);
    }

    @Override
    public void onCancleOrderFail(String error) {

    }
}